<?php

namespace App\Kuafu\User\Controller\B;

use App\Kuafu\Base\Controller\BaseController;
use App\Kuafu\User\Enum\UserCodeEnum;
use App\Kuafu\User\Service\AuthService;
use App\Kuafu\User\Validate\UserValidate;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class LoginController extends BaseController
{

    /**
     * 用户注册
     * @param Request $request
     * @return JsonResponse
     */
    public function register(Request $request)
    {
        $valid = UserValidate::register($request);
        if ($valid) {
            return $this->fail(UserCodeEnum::ERR_USER_INSERT, $valid);
        }
        AuthService::register($request);
        return $this->success();
    }

    /**
     * 用户登录
     * @param Request $request
     * @return JsonResponse
     */
    public function login(Request $request)
    {
        $valid = UserValidate::loginValid($request);
        if ($valid) {
            return $this->fail(UserCodeEnum::ERR_SERVER_ERROR, $valid);
        }
        $res = AuthService::login($request, UserCodeEnum::WEB_TYPE, $request->isLasting);
        return $this->success(['token' => $res]);
    }

    /**
     * 退出登录
     */
    public function logout(Request $request)
    {
        AuthService::logout();
        return $this->success();
    }


    public function refresh(Request $request)
    {
        $res = AuthService::refresh();
        return $this->success(['token' => $res]);
    }
}
